var registerPersonCtrl  = new XFApp.client();
$.views.converters({
	
});

XFApp.client.prototype.registerPerson = {
		successCode : 10000,
		urlItems : {			
			submitPersonURL: basePath+"/register/submitPerson",
			verifyCodeURL : basePath+"/verifyCode/creator?timestamp=",
			verifyCodeCheckURL: basePath+"/verifyCode/check",
			isExistURL: basePath+"/register/isExist",
			loginURL: basePath+"/login/login",		
		},
		
		formValidate : function(){
			$("#regForm").validate({
				errorPlacement : function(error, element) {
					if (element.is(":radio"))
						error.appendTo(element.parent());
					else if (element.is(":checkbox"))
						error.appendTo(element.parent().parent());
					else if(element.is(":input[name=fourcode]"))
						error.appendTo(element.parent());
					else
						error.insertAfter(element); 
						//error.appendTo(element.parent().next());
				},
		        rules: {
		        	//userId:"required",		        	         
		        	realName: "required",
		        	password: "required",
		        	password2: {
		        		        required:true,
		        		        equalTo: "#password"
		        	           },
		        	email:     {
		        		  		required:true,
		        		  		email: true
		        			   },
		        	mobile:    {
		        		       required:true,
		        		       number:true
		        	           },
		        	           
		        	//fourcode:"required", 
		        	                   			   
		        	accept:   "required"
				  },
				 messages: {
					 //userId:"请填写用户帐号",					          
			         realName: "请填写真实姓名",
			         password: "请填写密码",
			         password2:{
			        	 		required:"请再次输入密码",
			        	 		equalTo:"两次输入的密码不一致"
			         			},
			         email:     {
			        	 		required:"请填写电子邮件",
			        	 		email:"电子邮件格式不正确"
			         			},
			         mobile:    {
			        	         required:"请填写手机号码",
			        	         number:"手机号码格式不正确"
			        	        },
			         //fourcode:  "请输入验证码",			         			
			         accept:    "请接受服务协议"       	
				  }
		    });
			$("#regForm").validate();
		},
		
		
		loginFormValidate : function(){
			$("#loginForm2").validate({
				errorPlacement : function(error, element) {
					if (element.is(":radio"))
						error.appendTo(element.parent());
					else if (element.is(":checkbox"))
						error.appendTo(element.parent().parent());
					else if(element.is(":input"))
						error.appendTo(element.parent());
					else
						error.insertAfter(element); 
				},
		        rules: {
		        	userAccount2:   "required",
		        	userPassword2:  "required"
				  },
				 messages: {
					 userAccount2:   "请输入用户名",					           
					 userPassword2:  "请输入密码"
				  }
		    });
			$("#loginForm2").validate();
		},
		
		
		
		login : function (){			
			_this = this;			
			$("#loginBtn2").on("click",function(){				
				if($("#loginForm2").validate().form()){
					registerPersonCtrl.registerPerson.loged();					
				}				
			});
			//表单验证
			_this.loginFormValidate();			
		},	
		
		
		loged : function (){
			
			var entity = {
		        userId : $("#userAccount2").val(),
				password : $("#userPassword2").val(),
			}
	    	
	    	$.ajax({
	    		url :_this.urlItems.loginURL,
	    		data : entity,
	    		dataType : "json",
	    		type : 'post',
	    		success : function(data) {				
	    			if(data.code==_this.successCode){ 
	    				window.location.href= basePath;
	    			}else{
	    				window.webDialogUtil.alert("提示",data.msg);
	    			}
	    		}
	    	});	
		
		},
		
		
		event : function (){			
			_this = this;
			
			$("#submitBtn").on("click",function(){			
				
				if(!$("#userId").val()){
					$("#userId-error").html('请填写用户帐号');
				}
				
				if(!$("#fourcode").val()){
					$("#fourcode-error").html('请输入验证码');
				}
				
				if($("#regForm").validate().form()){
					if(!$("#userId-error").text() && !$("#fourcode-error").text()){						
						registerPersonCtrl.registerPerson.submit();					
					}
				}				
			});
			//表单验证
			_this.formValidate();
			
			$("#next").on("click",function(){
				_this.nextImg();
			});
			
			$("#userId").on("blur",function(){
				_this.checkAccount();
			});
		
			$("#fourcode").on("blur",function(){
				_this.verifyCodeCheck();
			});			
			
			//jquery validator 自定义方法
//			$.validator.addMethod("verifyCode", function(value, element) {				 
//				 return this.optional(element) || (_this.verifyCodeCheck(value));
//			});	
//			
//			$.validator.addMethod("accountCheck", function(value, element) {				
//				 return this.optional(element) || (_this.checkAccount(value));
//			});	
			
		},	
		
		verifyCodeCheck : function(){		
			var params = {
					verifyCode : $("#fourcode").val(),
					sessionKey:'verifyCode',
			}
			
			$.ajax({
	    		url :_this.urlItems.verifyCodeCheckURL,
	    		data : params,
	    		dataType : "json",
	    		type : 'post',
	    		async:false,
	    		success : function(data) {				
	    			if(data.code !=_this.successCode){ 
	    				$("#fourcode-error").html(data.msg);
	    			}else{
	    				$("#fourcode-error").html("");
	    			}
	    		}
	    	});
		},
		
		nextImg : function(){
			var url = _this.urlItems.verifyCodeURL+(new Date()).valueOf();
			$("#verifyCodeImg").attr("src",url);
		},
		
		
		 checkAccount : function(){
			var vo = {
			        userId : $("#userId").val()			
			}			
			
			$.ajax({
	    		url :_this.urlItems.isExistURL,
	    		data : vo,
	    		dataType : "json",
	    		type : 'post',
	    		async:false,
	    		success : function(data) {				
	    			if(data.code !=_this.successCode){
	    				$("#userId-error").html(data.msg);
	    			}else{
	    				$("#userId-error").html("");
	    			}
	    		}
	    	});		
		},
		
		submit : function (){
			
			var entity = {
			        userId : $("#userId").val(),
					realName : $("#realName").val()	,
					password : $("#password").val(),				
					email : $("#email").val(),
					mobile : $("#mobile").val(),
					registerType:"3", //3-个人用户
					activeState:"0",  //0-未激活
					auditState:"0"    //0-待审核
				}
		    	
		    	$.ajax({
		    		url :_this.urlItems.submitPersonURL,
		    		data : entity,
		    		dataType : "json",
		    		type : 'post',
		    		success : function(data) {				
		    			if(data.code==_this.successCode){ 
		    				window.webDialogUtil.alert("提示",data.msg);	    				
		    				//刷新	    		
		    				$("#regForm")[0].reset();
		    				//logistRecordCtrl.logistRecord.load();	    				
					    	        		
		    			}else{
		    				window.webDialogUtil.alert("提示",data.msg);
		    			}
		    		}
		    	});	
			
		},
},





$(function(){	
	
	//绑定回车事件
	$("#userPassword2").keydown(function(event) {		
		var key = event.keyCode || event.which;
		if (key == 13 || (key == 10 && $.browser.msie && $.browser.version <= 6.0)) {
			$("#loginBtn2").click();
		}
    });
	
	registerPersonCtrl.registerPerson.event();
	//登录
	registerPersonCtrl.registerPerson.login();
	
});